package com.aso.common.http.utils;

import android.text.TextUtils;

import com.aso.common.encrypt.AESUtils;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.aso.common.http.delegate.HttpConfig;
import com.itsdf07.alog.ALog;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Http请求工具类
 * Created by itsdf07 on 2017/7/20.
 */

public class HttpUtils {
    private static HttpUtils mInstance;
    private OkHttpClient mOkHttpClient;

    /**
     * 是否开启http数据加密
     */
    public static boolean isEncryptData = true;

    public static HttpUtils getInstance() {
        if (null == mInstance) {
            mInstance = new HttpUtils();
        }
        return mInstance;
    }

    private HttpUtils() {
        init();
    }

    private void init() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        //默认15秒连接超时
        builder.connectTimeout(HttpConfig.DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(HttpConfig.DEFAULT_TIMEOUT, TimeUnit.SECONDS);

//        File httpCacheDirectory = new File(FileUtils.INNERSDPATH, "http_cache");
//        Cache cache = new Cache(httpCacheDirectory, 10240 * 1024 * 100); //100M
//        builder.cache(cache);

        mOkHttpClient = builder.build();
    }

    public OkHttpClient getOkHttpClient() {
        return mOkHttpClient;
    }


    /**--------------------    异步数据请求    --------------------**/
    /**
     * @param url      请求地址
     * @param json     json数据格式
     * @param callback 请求回调
     * @Description POST提交JSON数据
     */
    public static void postAsyn(String url, String json, HttpCallbackImpl callback) {
        ALog.dTag(OkHttpRequest.TAG_HTTP, "url:%s，body(加密前):%s", url, json);
        if (TextUtils.isEmpty(json)) {
            return;
        }
        if (isEncryptData) {
            try {
                json = AESUtils.getInstance().encrypt(json);
                ALog.dTag("AES_encrypt", "加密后 json = %s", json);
            } catch (Exception e) {
                if (null != callback) {
                    callback.onFailure("-1", "HttpUtils postAsyn e = " + e.toString());
                }
                return;
            }
        }

        ALog.dTag(OkHttpRequest.TAG_HTTP, "url:%s，body(加密后):%s", url, json);
        Request request = OkHttpRequest.builderRequest(OkHttpRequest.HttpMethodType.POST, url, null, json);
        OkHttpRequest.doEnqueue(request, callback);
    }

    /**
     * 此接口不能加密
     *
     * @param url      请求地址
     * @param json     json数据格式
     * @param callback 请求回调
     * @Description POST提交JSON数据
     */

    public static void postAsyn(String url, String json, HttpCallbackImpl callback, MediaType type) {
        ALog.dTag(OkHttpRequest.TAG_HTTP, "url:%s，body:%s", url, json);
        Request request = OkHttpRequest.builderRequest(OkHttpRequest.HttpMethodType.POST, url, null, json, type);
        OkHttpRequest.doEnqueue(request, callback);
    }

    /**
     * @param url      请求地址
     * @param params   请求参数
     * @param callback 请求回调
     * @Description POST请求
     */
    public static void postAsyn(String url, Map<String, String> params, HttpCallbackImpl callback) {
        Request request = OkHttpRequest.builderRequest(OkHttpRequest.HttpMethodType.POST, url, params, null);
        OkHttpRequest.doEnqueue(request, callback);
    }

    /**--------------------    文件下载    --------------------**/
    /**
     * @param url          请求地址
     * @param destFileDir  目标文件存储的文件夹路径，如：Environment.getExternalStorageDirectory().getAbsolutePath()
     * @param destFileName 目标文件存储的文件名，如：gson-2.7.jar
     * @param callback     请求回调
     * @Description 文件下载
     */
    public static Call downloadAsynFile(String url, String destFileDir, String destFileName, HttpCallbackImpl callback) {
        Request request = OkHttpRequest.builderRequest(OkHttpRequest.HttpMethodType.POST, url, null, null);
        return OkHttpRequest.doDownloadEnqueue(request, destFileDir, destFileName, callback);
    }

}